package com.hyf.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.hyf.service.config.exception.GuliException;
import com.hyf.vod.service.VodService;
import com.hyf.vod.utils.ConstanVodutils;
import com.hyf.vod.utils.InitVodClient;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class VodServiceImpl implements VodService {

    // 上传视频到阿里云
    @Override
    public String uploadVideoAly(MultipartFile file) {

        try {
            // fileName ： 上传文件的原始名称
            String fileName=file.getOriginalFilename();
            // accessKeyId oss的ID
            //accessKeySecret oss的密钥
            // title ：阿里云上传之后显示的名称
            String title=fileName.substring(0, fileName.lastIndexOf("."));// 截取名称的一部分


            // inputStream ：上传文件的输入流

            InputStream inputStream= file.getInputStream();

            UploadStreamRequest request = new UploadStreamRequest(ConstanVodutils.ACCESS_KEY_ID, ConstanVodutils.ACCESS_KEY_SECRET, title, fileName, inputStream);

            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);// 通过 response 可以得到视频的 ID
            System.out.print("RequestId=" + response.getRequestId() + "\n");  //请求视频点播服务的请求ID

            String videoId=null;

            if (response.isSuccess()) {
                videoId=response.getVideoId() ;// 给 videoId 符值
                System.out.print("VideoId=" + response.getVideoId() + "\n");// 打印一下 videoId
            } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
                videoId=response.getVideoId();
                System.out.print("VideoId=" + response.getVideoId() + "\n");
                System.out.print("ErrorCode=" + response.getCode() + "\n");
                System.out.print("ErrorMessage=" + response.getMessage() + "\n");
            }
            return videoId;
        }catch (Exception e) {

            e.printStackTrace();// 打印一下异常
            throw new GuliException(20001, "guli vod 服务上传失败");

        }

    }




    //删除阿里云多个视频的方法
    @Override
    public void removeMoreAlyVideo(List videoIdList) {


        try {
            // 初始化对象
            DefaultAcsClient acsClient = InitVodClient.initVodClient(ConstanVodutils.ACCESS_KEY_ID, ConstanVodutils.ACCESS_KEY_SECRET);
            // 创建 删除视频 request对象
            DeleteVideoRequest request = new DeleteVideoRequest();

            // videoIdList 值 转换为 像1，2，3 一样的拼接的值
            String join = StringUtils.join(videoIdList.toArray(), ",");

            // 向 request 设置视频 id
            request.setVideoIds(join);
            // 调用对象的方法实现删除
             acsClient.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(20001,"删除视频失败");
        }

    }





    // 测试方法
    public static void main(String[] args) {
        // 先定义一个集合
        List<String> list=new ArrayList<>();

        //然后向这个 list 里面添加一些值
        list.add("11");
        list.add("33");
        list.add("22");
        // 使用一个方法 使他们编程用 逗号隔开的效果  org.apache.commons.lang 这里引入的包是这个，注意别引错了
        String join = StringUtils.join(list.toArray(), ",");
        // 打印输出看一下结果
        System.out.println(join);

    }












    // 删除多个阿里云视频，课件中的方法

 /*   @Override
    public void removeVideoList(List<String> videoIdList) {
        try {
            //初始化
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(
                    ConstantPropertiesUtil.ACCESS_KEY_ID,
                    ConstantPropertiesUtil.ACCESS_KEY_SECRET);
            //创建请求对象
            //一次只能批量删20个
            String str = org.apache.commons.lang.StringUtils.join(videoIdList.toArray(), ",");
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(str);
            //获取响应
            DeleteVideoResponse response = client.getAcsResponse(request);
            System.out.print("RequestId = " + response.getRequestId() + "\n");
        } catch (ClientException e) {
            throw new GuliException(20001, "视频删除失败");
        }
    }*/





//
//    // 删除阿里云 视频的方法 课件中的
//    @Override
//    public void removeVideo(String videoId) {
//        try{
//            DefaultAcsClient client = InitVodClient.initVodClient(
//                    ConstanVodutils.ACCESS_KEY_ID,
//                    ConstanVodutils.ACCESS_KEY_SECRET);
//            DeleteVideoRequest request = new DeleteVideoRequest();
//            request.setVideoIds(videoId);
//            DeleteVideoResponse response = client.getAcsResponse(request);
//            System.out.print("RequestId = " + response.getRequestId() + "\n");
//        }catch (ClientException e){
//            throw new GuliException(20001, "视频删除失败");
//        }
//    }








/*

    // 课件中的方法
     try {

        InputStream inputStream = file.getInputStream();

        String originalFilename = file.getOriginalFilename();

        String title = originalFilename.substring(0, originalFilename.lastIndexOf("."));

        UploadStreamRequest request = new UploadStreamRequest(

                ConstanVodutils.ACCESS_KEY_ID,

                ConstanVodutils.ACCESS_KEY_SECRET,

                title, originalFilename, inputStream);

        UploadVideoImpl uploader = new UploadVideoImpl();

        UploadStreamResponse response = uploader.uploadStream(request);

        //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。

        // 其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因

        String videoId = response.getVideoId();

        if (!response.isSuccess()) {

            String errorMessage = "阿里云上传错误：" + "code：" + response.getCode() + ", message：" + response.getMessage();

            log.warn(errorMessage);

            if(StringUtils.isEmpty(videoId)){

                throw new GuliException(20001, errorMessage);

            }

        }

        return videoId;

    } catch (IOException e) {

        throw new GuliException(20001, "guli vod 服务上传失败");
    }
}*/


}
